package com.github.catvod.spider.merge;

import com.github.catvod.spider.merge.HZ;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import javax.annotation.Nullable;

/* loaded from: classes.dex */
public abstract class fa implements Cloneable {
    static final List<fa> h = Collections.emptyList();

    @Nullable
    fa V0;
    int j;

    private static class Tw implements aC {
        private final HZ.Tw V0;
        private final Appendable h;

        Tw(Appendable appendable, HZ.Tw tw) {
            this.h = appendable;
            this.V0 = tw;
            tw.W();
        }

        @Override // com.github.catvod.spider.merge.aC
        public void V0(fa faVar, int i) {
            try {
                faVar.Og(this.h, i, this.V0);
            } catch (IOException e) {
                throw new zx(e);
            }
        }

        @Override // com.github.catvod.spider.merge.aC
        public void h(fa faVar, int i) {
            if (faVar.PU().equals("#text")) {
                return;
            }
            try {
                faVar.R(this.h, i, this.V0);
            } catch (IOException e) {
                throw new zx(e);
            }
        }
    }

    protected fa() {
    }

    private void l(int i) {
        List<fa> iU = iU();
        while (i < iU.size()) {
            iU.get(i).FE(i);
            i++;
        }
    }

    public fa BQ(fa faVar) {
        R2.u(faVar);
        R2.u(this.V0);
        this.V0.V0(this.j, faVar);
        return this;
    }

    @Nullable
    public fa C4() {
        return this.V0;
    }

    public void F(String str) {
        R2.u(str);
        t(str);
    }

    protected fa F8(@Nullable fa faVar) {
        try {
            fa faVar2 = (fa) super.clone();
            faVar2.V0 = faVar;
            faVar2.j = faVar == null ? 0 : this.j;
            return faVar2;
        } catch (CloneNotSupportedException e) {
            throw new RuntimeException(e);
        }
    }

    protected void FE(int i) {
        this.j = i;
    }

    @Nullable
    public fa GM() {
        fa faVar = this.V0;
        if (faVar == null) {
            return null;
        }
        List<fa> iU = faVar.iU();
        int i = this.j + 1;
        if (iU.size() > i) {
            return iU.get(i);
        }
        return null;
    }

    protected void H9(fa faVar) {
        R2.u(faVar);
        fa faVar2 = this.V0;
        if (faVar2 != null) {
            faVar2.rd(this);
        }
        this.V0 = faVar;
    }

    public abstract QF I();

    @Nullable
    public fa Ig() {
        fa faVar = this.V0;
        if (faVar != null && this.j > 0) {
            return faVar.iU().get(this.j - 1);
        }
        return null;
    }

    protected void LW(Appendable appendable) {
        bY.V0(new Tw(appendable, n.h(this)), this);
    }

    abstract void Og(Appendable appendable, int i, HZ.Tw tw);

    public boolean PD() {
        return this.V0 != null;
    }

    public abstract String PU();

    public List<fa> Qy() {
        if (u() == 0) {
            return h;
        }
        List<fa> iU = iU();
        ArrayList arrayList = new ArrayList(iU.size());
        arrayList.addAll(iU);
        return Collections.unmodifiableList(arrayList);
    }

    abstract void R(Appendable appendable, int i, HZ.Tw tw);

    protected void T(fa faVar) {
        faVar.H9(this);
    }

    public void To(fa faVar) {
        R2.u(faVar);
        R2.u(this.V0);
        this.V0.V(this, faVar);
    }

    protected void V(fa faVar, fa faVar2) {
        R2.j(faVar.V0 == this);
        R2.u(faVar2);
        fa faVar3 = faVar2.V0;
        if (faVar3 != null) {
            faVar3.rd(faVar2);
        }
        int i = faVar.j;
        iU().set(i, faVar2);
        faVar2.V0 = this;
        faVar2.FE(i);
        faVar.V0 = null;
    }

    protected void V0(int i, fa... faVarArr) {
        boolean z;
        R2.u(faVarArr);
        if (faVarArr.length == 0) {
            return;
        }
        List<fa> iU = iU();
        fa C4 = faVarArr[0].C4();
        if (C4 != null && C4.u() == faVarArr.length) {
            List<fa> iU2 = C4.iU();
            int length = faVarArr.length;
            while (true) {
                int i2 = length - 1;
                if (length <= 0) {
                    z = true;
                    break;
                } else {
                    if (faVarArr[i2] != iU2.get(i2)) {
                        z = false;
                        break;
                    }
                    length = i2;
                }
            }
            if (z) {
                C4.n();
                iU.addAll(i, Arrays.asList(faVarArr));
                int length2 = faVarArr.length;
                while (true) {
                    int i3 = length2 - 1;
                    if (length2 <= 0) {
                        l(i);
                        return;
                    } else {
                        faVarArr[i3].V0 = this;
                        length2 = i3;
                    }
                }
            }
        }
        R2.I(faVarArr);
        for (fa faVar : faVarArr) {
            T(faVar);
        }
        iU.addAll(i, Arrays.asList(faVarArr));
        l(i);
    }

    public fa W(int i) {
        return iU().get(i);
    }

    @Nullable
    public HZ b() {
        fa z = z();
        if (z instanceof HZ) {
            return (HZ) z;
        }
        return null;
    }

    public boolean equals(Object obj) {
        return this == obj;
    }

    @Nullable
    public final fa f() {
        return this.V0;
    }

    public String h(String str) {
        R2.BQ(str);
        return (y() && I().n(str)) ? ZG.n(oC(), I().F8(str)) : "";
    }

    public int hr() {
        return this.j;
    }

    public boolean i4(String str) {
        R2.u(str);
        if (!y()) {
            return false;
        }
        if (str.startsWith("abs:")) {
            String substring = str.substring(4);
            if (I().n(substring) && !h(substring).isEmpty()) {
                return true;
            }
        }
        return I().n(str);
    }

    protected abstract List<fa> iU();

    public String j(String str) {
        R2.u(str);
        if (!y()) {
            return "";
        }
        String F8 = I().F8(str);
        return F8.length() > 0 ? F8 : str.startsWith("abs:") ? h(str.substring(4)) : "";
    }

    protected void j5(Appendable appendable, int i, HZ.Tw tw) {
        appendable.append('\n').append(ZG.F8(i * tw.oC()));
    }

    public abstract fa n();

    public void nf() {
        R2.u(this.V0);
        this.V0.rd(this);
    }

    public abstract String oC();

    @Override // 
    public fa oq() {
        fa F8 = F8(null);
        LinkedList linkedList = new LinkedList();
        linkedList.add(F8);
        while (!linkedList.isEmpty()) {
            fa faVar = (fa) linkedList.remove();
            int u = faVar.u();
            for (int i = 0; i < u; i++) {
                List<fa> iU = faVar.iU();
                fa F82 = iU.get(i).F8(faVar);
                iU.set(i, F82);
                linkedList.add(F82);
            }
        }
        return F8;
    }

    public fa q(String str, String str2) {
        I().LW(n.V0(this).q().h(str), str2);
        return this;
    }

    protected void rd(fa faVar) {
        R2.j(faVar.V0 == this);
        int i = faVar.j;
        iU().remove(i);
        l(i);
        faVar.V0 = null;
    }

    protected abstract void t(String str);

    public String toString() {
        return w4();
    }

    public abstract int u();

    public List<fa> v() {
        fa faVar = this.V0;
        if (faVar == null) {
            return Collections.emptyList();
        }
        List<fa> iU = faVar.iU();
        ArrayList arrayList = new ArrayList(iU.size() - 1);
        for (fa faVar2 : iU) {
            if (faVar2 != this) {
                arrayList.add(faVar2);
            }
        }
        return arrayList;
    }

    public String w4() {
        StringBuilder V0 = ZG.V0();
        LW(V0);
        return ZG.t(V0);
    }

    protected abstract boolean y();

    void yx() {
    }

    public fa z() {
        fa faVar = this;
        while (true) {
            fa faVar2 = faVar.V0;
            if (faVar2 == null) {
                return faVar;
            }
            faVar = faVar2;
        }
    }
}
